package Form

import (
	"clientDemo/ui/walk_ui/widget"
	"github.com/lxn/walk"
	"github.com/lxn/walk/declarative"
	"github.com/lxn/win"
	"math/rand"
	"strconv"
	"unsafe"
)

func (m *FormModule) MainForm() {
	mw, err := walk.NewMainWindowWithName("main_form")

	if err != nil {
		walk.MsgBox(mw, "错误", "程序打开失败", walk.MsgBoxIconError)
		walk.App().Exit(0)
		return
	}

	declarative.MainWindow{
		AssignTo: &mw,
		Title:    "主窗口",
		Size:     declarative.Size{Width: loginGUIWidth, Height: loginGUIHeight},
		Layout:   declarative.VBox{},
		Children: []declarative.Widget{
			declarative.Composite{
				Layout: declarative.Grid{Columns: 2},
				Children: []declarative.Widget{
					declarative.Label{
						Text: "                        主窗口信息：",
					},
					declarative.LineEdit{
						MaxSize:  declarative.Size{Width: 150, Height: 40},
						Text:     "这是主窗口",
						ReadOnly: true,
					},
				},
			},
			declarative.Composite{
				Layout: declarative.HBox{},
				Children: []declarative.Widget{
					declarative.PushButton{
						Text:      "按钮",
						MaxSize:   declarative.Size{Width: 50, Height: 40},
						OnClicked: funcBT(),
					},
				},
			},
		},
	}.Create()

	centerWindow(mw)
	mw.Run()
	// 托盘
	//setSysTray(m.logger, mw)
}

func centerWindow(window *walk.MainWindow) {
	handle := window.Handle()
	monitor := win.MonitorFromWindow(handle, win.MONITOR_DEFAULTTONEAREST)
	var mi win.MONITORINFO
	mi.CbSize = uint32(unsafe.Sizeof(mi))
	win.GetMonitorInfo(monitor, &mi)

	screenWidth := mi.RcWork.Right - mi.RcWork.Left
	screenHeight := mi.RcWork.Bottom - mi.RcWork.Top

	windowWidth := int32(window.Width())
	windowHeight := int32(window.Height())

	x := mi.RcWork.Left + (screenWidth-windowWidth)/2
	y := mi.RcWork.Top + (screenHeight-windowHeight)/2

	win.SetWindowPos(handle, 0, x, y, windowWidth, windowHeight, win.SWP_NOZORDER|win.SWP_NOACTIVATE)
}

func funcBT() func() {
	return func() {

		print("TestItem.TestC1.TestC()")
	}
}

func (m *FormModule) MainForm2() {
	mw, err := walk.NewMainWindowWithName("main_form")
	if err != nil {
		walk.MsgBox(mw, "错误", "程序打开失败", walk.MsgBoxIconError)
		walk.App().Exit(0)
		return
	}

	// 初始化数据模型
	m.model = &widget.TableModel{}

	for i := 0; i < 10; i++ {
		id := rand.Intn(1000) // 生成随机 ID
		name := "Name " + strconv.Itoa(id)
		m.model.Items = append(m.model.Items, widget.Item{ID: id, Name: name}) // 向模型中添加新数据
	}

	declarative.MainWindow{
		AssignTo: &mw,
		Title:    "主窗口",
		Size:     declarative.Size{Width: loginGUIWidth, Height: loginGUIHeight},
		Layout:   declarative.VBox{},
		Children: []declarative.Widget{
			declarative.TableView{
				AssignTo: &m.tableView,
				Columns: []declarative.TableViewColumn{
					{Title: "ID", Width: 100},
					{Title: "Name", Width: 200},
				},
				//Model: m.model,
			},
			declarative.Composite{
				Layout: declarative.HBox{},
				Children: []declarative.Widget{
					declarative.PushButton{
						Text:      "插入数据",
						OnClicked: m.addData,
					},
				},
			},
		},
	}.Create()

	centerWindow(mw)

	//tableView.SetModel(m.model)
	mw.Run()
	// 托盘
	//setSysTray(m.logger, mw)
}

func (m *FormModule) addData() {
	for i := 0; i < 10; i++ {
		id := rand.Intn(1000) // 生成随机 ID
		name := "Name " + strconv.Itoa(id)
		m.model.Items = append(m.model.Items, widget.Item{ID: id, Name: name}) // 向模型中添加新数据
	}
	m.tableView.SetModel(m.model) // 更新表格模型
}
